
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>全选/取消全选，反选/取消反选</title>
</head>
<body>
</p>
<p><input type="checkbox" id="selectAll">全选/取消全选<input type="checkbox" id="investselect">反选/取消反选</p>
<p>
    <input type="checkbox" name="checkmost">
    <input type="checkbox" name="checkmost">
    <input type="checkbox" name="checkmost">
    <input type="checkbox" name="checkmost">
    <input type="checkbox" name="checkmost">
    <input type="checkbox" name="checkmost">

</p>
<script>
    window.onload=function(){
        var targets=document.getElementsByName("checkmost"),
            selectAll=document.getElementById("selectAll"),
            investselect=document.getElementById("investselect");
        selectAll.onclick=function(){
            loop(targets,"all",this);
        }
        investselect.onclick=function(){
            loop(targets,"invest",this);
        }
    }
    function loop(objs,flag,that){
        var select=null;
        for(var i= 0,length=objs.length;i<length;i++){
            switch(flag) {
                case "all": select=that.checked; break;
                case "invest": select=!objs[i].checked; break;
            }
            objs[i].checked=select;
        }
    }
</script>
</body>
</html>
